
function onMouseDown(event){
    _gCanvas.addEventListener('mousemove',onMouseMove,false);
    _gCanvas.addEventListener('mouseup',onMouseUp,false);
    input.down = true;
    input.onTouchBegin(event);
}
function onMouseMove(event) {
    input.onTouchMove(event);
}
function onMouseUp(event){
    _gCanvas.removeEventListener('mousemove',onMouseMove);
    input.down = false;
    input.onTouchEnd(event);
}

function onMouseEnter(event){
    // console.log('mouse enter====');
    _gCanvas.addEventListener('mousedown',onMouseDown,false);
}

function onMouseLeave(event){
    // console.log('mouse leave====');
    _gCanvas.removeEventListener('mousedown',onMouseDown);
    _gCanvas.removeEventListener('mouseup',onMouseUp);
    if(input.down){
        _gCanvas.removeEventListener('mousemove',onMouseMove);
        input.down = false;
        input.onTouchCancel(event);
    }
}


let _gCanvas;
let input = {

    down:false,
    lstObserver:[],
    downPos:null,
    deltaPos:null,
    lastPos:null,

    init(canvas) {
        _gCanvas = this.canvas = canvas;
        canvas.addEventListener('mouseenter',onMouseEnter,false);
        canvas.addEventListener('mouseleave',onMouseLeave,false);
        this.downPos = cc.v2();
        this.deltaPos = cc.v2();
        this.lastPos = cc.v2();
    },

    addObserver(obj){
        let idx = this.lstObserver.indexOf(obj);
        if(idx >= 0){
            throw  new Error('已经添加过 obj!');
        }
        this.lstObserver.push(obj);
    },

    onTouchBegin(event){
        // 需要转换成逻辑坐标
        // let x = (event.clientX - this.canvas.offsetLeft)/2;
        // let y = app.height - (event.clientY - this.canvas.offsetTop)/2;
        // console.log(x,y);
        // console.log('#######touch begin ****');
        let pos = this.getPosFromEvent(event);
        cc.Vec2.set(this.downPos,pos.x,pos.y);
        cc.Vec2.set(this.lastPos,pos.x,pos.y);
        this.lstObserver.forEach(obj=>{
           if(obj.touchBegin){
               obj.touchBegin(pos);
           }
        });
    },
    onTouchMove(event){
        // console.log('#######touch move >>>>>');
        let pos = this.getPosFromEvent(event);
        cc.Vec2.set(this.deltaPos,pos.x - this.lastPos.x,pos.y - this.lastPos.y);
        cc.Vec2.copy(this.lastPos,pos);
        this.lstObserver.forEach(obj=>{
            if(obj.touchMove){
                obj.touchMove(pos,this.deltaPos);
            }
        });
    },
    onTouchEnd(event){
        // console.log('touch end called====');
        let pos = this.getPosFromEvent(event);
        this.lstObserver.forEach(obj=>{
            if(obj.touchEnd){
                obj.touchEnd(pos);
            }
        });
    },
    onTouchCancel(event){
        // console.log('touch cancel called!');
        let pos = this.getPosFromEvent(event);
        this.lstObserver.forEach(obj=>{
            if(obj.touchCancel){
                obj.touchCancel(pos);
            }
        });
    },

    /**
     * 获取 逻辑坐标.
     * @param event
     * @return {*}
     */
    getPosFromEvent(event){
        let x = (event.clientX - this.canvas.offsetLeft)/2;
        let y = app.height - (event.clientY - this.canvas.offsetTop)/2;
        return cc.v2(x,y);
    }
};


/**
 *
 *  input.add(camera);
 */

export default input;
